function withData(param) {
    return param < 10 ? '0' + param : '' + param;
} // 区分出年月日

function getLoopArray(starts, ends, type) {
    let start = starts || 0;
    let end = ends || 1;
    let array = [];

    if (type == 1) {
        for (let i = start; i <= end; i++) {
            array.push(withData(i) + '年');
        }
    } else if (type == 2) {
        for (let i = start; i <= end; i++) {
            array.push(withData(i) + '月');
        }
    } else {
        array = ['--'];

        for (let i = start; i <= end; i++) {
            array.push(withData(i) + '日');
        }
    }

    return array;
} // 处理天数

function getMonthDay(year, month) {
    let flag = year % 400 == 0 || (year % 4 == 0 && year % 100 != 0),
        array = null;

    switch (month) {
        case '01':
        case '03':
        case '05':
        case '07':
        case '08':
        case '10':
        case '12':
            array = getLoopArray(1, 31);
            break;

        case '04':
        case '06':
        case '09':
        case '11':
            array = getLoopArray(1, 30);
            break;

        case '02':
            array = flag ? getLoopArray(1, 29) : getLoopArray(1, 28);
            break;

        default:
            array = '月份格式不正确，请重新输入！';
    }

    console.log(array);
    return array;
}

function getNewDateArry(type) {
    // 当前时间的处理
    let newDate = new Date();
    let year = withData(newDate.getFullYear()),
        mont = withData(newDate.getMonth() + 1),
        date = withData(newDate.getDate()),
        hour = withData(newDate.getHours()),
        minu = withData(newDate.getMinutes()),
        seco = withData(newDate.getSeconds()); // 默认日期

    if (type) {
        return [year + '年', mont + '月', date + '日'];
    } else {
        return [year, mont, date, hour, minu, seco];
    }
}

function dateTimePicker(startYear, endYear, date) {
    // 返回默认显示的数组和联动数组的声明
    let dateTime = [],
        dateTimeArray = [[], [], [], [], [], []];
    let start = startYear || 1978;
    let end = endYear || 2100; // 默认开始显示数据

    let defaultDate = date ? [...date.split(' ')[0].split('-'), ...date.split(' ')[1].split(':')] : getNewDateArry(); // 处理联动列表数据

    /*年月日 时分秒*/

    dateTimeArray[0] = getLoopArray(start, end, 1);
    dateTimeArray[1] = getLoopArray(1, 12, 2);
    dateTimeArray[2] = getMonthDay(defaultDate[0], defaultDate[1]);
    dateTimeArray[3] = getLoopArray(0, 23);
    dateTimeArray[4] = getLoopArray(0, 59);
    dateTimeArray[5] = getLoopArray(0, 59);
    dateTimeArray.forEach((current, index) => {
        dateTime.push(current.indexOf(defaultDate[index]));
    });
    return {
        dateTimeArray: dateTimeArray,
        dateTime: dateTime
    };
} // 重写年月日格式

function newDateTimePicker(startYear, endYear, date) {
    // 返回默认显示的数组和联动数组的声明
    let dateTime = [],
        dateTimeArray = [[], [], []];
    let start = startYear || 1978; // 开始时间

    let end = endYear || 2100; // 结束时间
    // 默认开始显示数据

    let defaultDate = date ? [...date.split(' ')[0].split('-'), ...date.split(' ')[1].split(':')] : getNewDateArry('true'); // 处理联动列表数据

    /*年月日 时分秒*/

    dateTimeArray[0] = getLoopArray(start, end, 1);
    dateTimeArray[1] = getLoopArray(1, 12, 2);
    dateTimeArray[2] = getMonthDay(defaultDate[0].split('年')[0], defaultDate[1].split('月')[0]);
    dateTimeArray.forEach((current, index) => {
        dateTime.push(current.indexOf(defaultDate[index]));
    });
    return {
        dateTimeArray,
        dateTime
    };
}

module.exports = {
    dateTimePicker,
    newDateTimePicker,
    getMonthDay
};
